package com.ruoyi.system.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.dto.MapDto;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;

@Component
public class BaiDuMapUtil {

    @Value("${baidu.ak}")
    private String ak;

    @Value("${baidu.url}")
    private String url;

    @Value("${baidu.output}")
    private String outPut;

    public JSONObject location(String address){
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("address", address);
        paramMap.put("output",this.outPut);
        paramMap.put("ak",this.ak);
        String result = HttpUtil.get(this.url, paramMap);
        JSONObject info = JSONObject.parseObject(result);
        JSONObject location = new JSONObject();
        if (info.containsKey("status") && info.getInteger("status") == 0){
            location = info.getJSONObject("result").getJSONObject("location");
        }
        return location;
    }




    public static void writeWordFile(String path, String fileName, String content) throws Exception {

        File fileDir = new File(path);
        if (fileDir.exists()) {
            byte b[] = content.getBytes();
            ByteArrayInputStream bais = new ByteArrayInputStream(b);
            POIFSFileSystem poifs = new POIFSFileSystem();
            DirectoryEntry directory = poifs.getRoot();
            DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
            FileOutputStream ostream = new FileOutputStream(path + fileName);
            poifs.writeFilesystem(ostream);
            bais.close();
            ostream.close();
        }
    }

    /**
     * 通过经纬度获取地址
     * @param dto
     * @return
     */
    public static  String getAddress(MapDto dto) {
        String url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak=fYtd32cSOSGLe2xurHvZQDtABPjorucT&output=json&coordtype=wgs84ll&location=";
        url+=dto.getLat()+","+dto.getLon();
        String result = HttpUtil.get(url);
        JSONObject object = JSONObject.parseObject(result);
        String address = object.getJSONObject("result").getString("formatted_address");
        return address;
    }



    /**
     * 根据地址经过百度地图查询经纬度
     *
     * @param address 地址
     * @return 返回地址的经纬度
     */
    public static JSONObject geocoding(String address) {

        String addr ="河北省邢台市"+address;

        String ak ="2895bTSE7xtmHZMGMtNYlp48LSdAXiOd";
        String url = "https://api.map.baidu.com/geocoding/v3/?address=" + addr +
                "&output=json&ak=" + ak + "&callback=showLocation&ret_coordtype=gcj02ll";
        String result = HttpUtil.get(url);
        return getLngAndLat(result);
    }

    /**
     *  处理百度地图放回结果集 获取经纬度
     * @param params 参数
     * @return
     */
    public static JSONObject getLngAndLat(String params) {
        JSONObject resultJsonObj = new JSONObject();
        String data = params.replaceAll("showLocation&&showLocation\\(", "").replaceAll("\\)","");
        JSONObject jsonObject1 = JSON.parseObject(data);
        JSONObject resultJson = jsonObject1.getJSONObject("result");
        if(null != resultJson){
            JSONObject locationJson = resultJson.getJSONObject("location");
            resultJsonObj.put("lng",locationJson.getString("lng"));
            resultJsonObj.put("lat",locationJson.getString("lat"));
            return resultJsonObj;
        }else{
            return null;
        }
    }

    public static void main(String[] args) {
        String str ="辛集市东华路107号," +
                "辛集市裴辛庄村," +
                "河北省辛集市张古庄镇北陈位村中王路74号";
        String[] split = str.split(",");
        for (int i = 0; i < split.length; i++) {
        System.out.println(split[i]+"====="+geocoding(split[i]));
        }

    }
}